package com.bussiness.system.controller;

import com.bussiness.system.dao.MyDeptPostsDao;
import com.bussiness.system.dao.MyDeptRoleDao;
import com.bussiness.system.dao.MyDeptUserDao;
import com.bussiness.system.dao.MyUserDao;
import com.bussiness.system.dto.MyUserDto;
import com.bussiness.system.model.MyDeptPostsEntity;
import com.bussiness.system.model.MyUserEntity;
import com.bussiness.system.service.DeptPostsRoleService;
import com.bussiness.system.dto.MyDeptPostsDto;
import com.bussiness.system.dto.MyDeptRoleDto;
import com.bussiness.system.dto.MyDeptUserDto;
import com.common.response.ResponseBean;
import com.common.response.impl.BaseResponseImpl;
import com.common.util.mypage.Pages;
import com.common.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author zyj
 * @description
 * @date 2019/9/24 9:57
 **/
@RestController
@RequestMapping("/deptPostsRole")
@Api(tags = {"部门人员角色"})
public class DeptPostsRoleController extends BaseResponseImpl {
    Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    public MyDeptPostsDao myDeptPostsDao;
    @Autowired
    public MyDeptRoleDao myDeptRoleDao;
    @Autowired
    public MyDeptUserDao myDeptUserDao;
    @Autowired
    public DeptPostsRoleService deptPostsRoleService;

    @Autowired
    private MyUserDao myUserDao;


    @ApiOperation(value = "添加公司、子公司、部门、岗位")
    @PutMapping(value = "/addDept" )
    public ResponseBean addDept(@RequestBody MyDeptPostsDto myDeptPostsDto){
        try {
            MyDeptPostsEntity MyDeptPostsEntity = new MyDeptPostsEntity(myDeptPostsDto);
            myDeptPostsDao.insertSelective(MyDeptPostsEntity);
            return success();
        }catch (Exception e){
            log.error("添加公司、子公司、部门、岗位失败",e);
            return errorSave();
        }

    }

    @ApiOperation(value = "修改公司、子公司、部门、岗位")
    @PutMapping(value = { "/updateDept" })
    public ResponseBean updateDept(@RequestBody MyDeptPostsDto myDeptPostsDto){
        try {
            MyDeptPostsEntity myDeptPostsEntity = myDeptPostsDao.selectByPrimaryKey(Long.parseLong(myDeptPostsDto.getId()));
            myDeptPostsEntity.updateEntity(myDeptPostsEntity, myDeptPostsDto);
            myDeptPostsDao.updateByPrimaryKeySelective(myDeptPostsEntity);
            return success();
        }catch (Exception e){
            log.error("修改公司、子公司、部门、岗位失败",e);
            return errorUpdate();
        }
    }

    @ApiOperation(value = "根据上级id获取下级id列表")
    @GetMapping(value ="/getDeptList")
    public ResponseBean getDeptList(@RequestParam Long parentId, Integer pageSize, Integer pageNumber,String deptName){
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("parentId", parentId);
            params.put("deptName", deptName);
            Pages pages = new Pages(pageNumber, pageSize);
            pages.setParams(params);
            pages.setRows(myDeptPostsDao.pageDeptByParentId(pages));
            return successData(pages);
        }catch (Exception e){
            log.error("根据上级id获取下级id列表",e);
            return error();
        }
    }

    @ApiOperation(value = "获取公司、子公司、部门、岗位树")
    @RequestMapping(value ="/getDeptTree", method = RequestMethod.GET)
    public ResponseBean getDeptTree(@RequestParam String prentId){
        try {
            return successData(deptPostsRoleService.getTreeVo(prentId));
        }catch (Exception e){
            log.error("获取公司、子公司、部门、岗位list",e);
            return error();
        }
    }

    @ApiOperation(value = "根据部门id获取部门角色列表")
    @GetMapping(value ="/getDeptRole")
    public ResponseBean getDeptRole(String deptId){
        try {
            List<TreeVo> lists = deptPostsRoleService.getDeptRoleTreeList(Long.parseLong(deptId));
            return successData(lists);
        }catch (Exception e){
            log.error("根据部门id获取部门角色列表失败",e);
            return error();
        }
    }

    @ApiOperation(value = "修改部门角色")
    @PutMapping(value ="/updateDeptRole")
    public ResponseBean updateDeptRole(@RequestBody List<MyDeptRoleDto> myDeptRoleDto){
        try {
            deptPostsRoleService.updateDeptRole(myDeptRoleDto);
            return success();
        }catch (Exception e){
            log.error("修改部门角色失败",e);
            return errorSave();
        }
    }

    @ApiOperation(value = "根据部门id获取部门人员列表")
    @GetMapping(value ="/getDeptUser")
    public ResponseBean getDeptUser(String deptId){
        try {
            List<TreeVo> list = deptPostsRoleService.getUserTreeList(Long.parseLong(deptId));
            return successData(list);
        }catch (Exception e){
            log.error("获取部门人员列表失败",e);
            return error();
        }
    }

    @ApiOperation(value = "修改部门角色")
    @PutMapping(value ="/updateDeptUser")
    public ResponseBean updateDeptUser(@RequestBody List<MyDeptUserDto> myDeptUserDtos){
        try {
            deptPostsRoleService.updateDeptUser(myDeptUserDtos);
            return success();
        }catch (Exception e){
            log.error("修改部门角色失败",e);
            return errorSave();
        }
    }

    @ApiOperation(value = "获取用户信息")
    @GetMapping(value="/userList")
    public ResponseBean userList(Integer pageSize, Integer pageNumber,String userName){
        try {
            Pages pages = new Pages(pageNumber, pageSize);
            Map<String, Object> param = new HashMap<>();
            param.put("userName", userName);
            pages.setParams(param);
            pages.setRows(myUserDao.pageUser(pages));
            return successData(pages);
        }catch (Exception e){
            log.error("获取用户信息失败",e);
            return errorSave();
        }
    }

    @ApiOperation(value = "根据id获取用户信息")
    @GetMapping(value="/userInfo")
    public ResponseBean userInfo(Integer userId){
        try {
            MyUserEntity myUserEntity = myUserDao.selectByPrimaryKey(userId);
            return successData(myUserEntity);
        }catch (Exception e){
            log.error("根据id获取用户信息失败",e);
            return errorSave();
        }
    }


    @ApiOperation(value = "添加用户信息")
    @RequestMapping(value="/addUser", method = RequestMethod.PUT)
    public ResponseBean addUser(@RequestBody MyUserDto securityUserDto){
        try {
            MyUserEntity securityUserEntity = new MyUserEntity();
            BeanUtils.copyProperties(securityUserDto, securityUserEntity);
            myUserDao.insertSelective(securityUserEntity);
            return success();
        }catch (Exception e){
            log.error("添加用户信息失败",e);
            return errorSave();
        }
    }

    @ApiOperation(value = "更新用户信息")
    @RequestMapping(value="/updateUser", method = RequestMethod.PUT)
    public ResponseBean updateUser(@RequestBody MyUserDto securityUserDto){
        try {
            MyUserEntity securityUserEntity = new MyUserEntity();
            BeanUtils.copyProperties(securityUserDto, securityUserEntity);
            myUserDao.updateByPrimaryKeySelective(securityUserEntity);
            return success();
        }catch (Exception e){
            log.error("更新用户信息失败",e);
            return errorSave();
        }
    }

    @ApiOperation(value = "删除用户信息")
    @RequestMapping(value="/deleteUser", method = RequestMethod.DELETE)
    public ResponseBean deleteUser(Integer userId){
        try {
            myUserDao.deleteByPrimaryKey(userId);
            return success();
        }catch (Exception e){
            log.error("删除用户信息失败",e);
            return errorSave();
        }
    }
}
